package com.xyzq.webapp.action.ya.report;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.xyzq.model.common.DataDictionary;
import com.xyzq.model.ya.data.Account;
import com.xyzq.model.ya.data.AccountDayFund;
import com.xyzq.model.ya.data.AccountDayMoney;
import com.xyzq.model.ya.data.AccountDayStock;
import com.xyzq.model.ya.data.Staff;
import com.xyzq.model.ya.report.StaffAccountAssert;
import com.xyzq.service.common.DataDictionaryManager;
import com.xyzq.service.ya.AccountDayFundManager;
import com.xyzq.service.ya.AccountDayMoneyManager;
import com.xyzq.service.ya.AccountDayStockManager;
import com.xyzq.service.ya.AccountManager;
import com.xyzq.service.ya.StaffManager;
import com.xyzq.webapp.action.BaseAction;

public class StaffAccountAssertReportAction extends BaseAction implements
		Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5936550587988495735L;

	private Date start;
	private Date end;
	private Date valueDay;
	private String type;
	private List<DataDictionary> typeList;
	private HashMap<Staff, StaffAccountAssert> staffAccountAssertMap;
	private List<Account> accountList;

	private DataDictionaryManager dataDictionaryManager;
	private AccountManager accountManager;
	private StaffManager staffManager;
	private AccountDayMoneyManager accountDayMoneyManager;
	private AccountDayFundManager accountDayFundManager;
	private AccountDayStockManager accountDayStockManager;

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		this.typeList = this.dataDictionaryManager
				.findByModule("openAccountType");
	}

	public String execute() {
		if (start == null || end == null || valueDay == null || type == null) {
			return INPUT;
		}

		if (type.equals("资金帐户")) {
			this.accountList = this.accountManager
					.findAccountByDate(start, end);
		} else if (type.equals("上海指定")) {
			this.accountList = this.accountManager.findShAccountByDate(start,
					end);
		} else if (type.equals("上海未指定")) {
			this.accountList = this.accountManager.findSzAccountByDate(start,
					end);
		}

		if (this.accountList == null) {
			return INPUT;
		}

		this.staffAccountAssertMap = new HashMap<Staff, StaffAccountAssert>();

		for (Staff staff : this.staffManager.getAll()) {
			StaffAccountAssert saa = new StaffAccountAssert(staff);
			this.staffAccountAssertMap.put(staff, saa);
		}

		for (Account account : this.accountList) {
			StaffAccountAssert saa = this.staffAccountAssertMap.get(account
					.getStaff());
			if (saa.getDay() == null) {
				saa.setDay(valueDay);
			}
			List<AccountDayMoney> admList = this.accountDayMoneyManager
					.findWithValueDay(valueDay, account);
			for (AccountDayMoney adm : admList) {
				if (adm.getType().equals("人民币")) {
					saa.setRMB(saa.getRMB() + adm.getMoney());
				}
				if (adm.getType().equals("港币")) {
					saa.setHK(saa.getHK() + adm.getMoney());
				}
				if (adm.getType().equals("美元")) {
					saa.setUSA(saa.getUSA() + adm.getMoney());
				}
				if (adm.getType().equals("信用")) {
					saa.setCreditCash(saa.getCreditCash() + adm.getMoney());
				}
				if (adm.getType().equals("多银行")) {
					saa.setMutilBank(saa.getMutilBank() + adm.getMoney());
				}
			}

			List<AccountDayFund> adfList = this.accountDayFundManager
					.findWithValueDay(valueDay, account);
			for (AccountDayFund adf : adfList) {
				saa.setFund(saa.getFund() + adf.getWorth());
			}

			List<AccountDayStock> adsList = this.accountDayStockManager
					.findWithValueDay(valueDay, account);
			for (AccountDayStock ads : adsList) {
				if (ads.getType().equals("ETF基金")) {
					saa.setETF(saa.getETF() + ads.getWorth());
				} else if (ads.getType().equals("LOF基金")) {
					saa.setLOF(saa.getLOF() + ads.getWorth());
				} else if (ads.getType().equals("交易权证")) {
					saa.setWarrant(saa.getWarrant() + ads.getWorth());
				} else if (ads.getType().equals("企业债券")) {
					saa.setCorporateBond(saa.getCorporateBond()
							+ ads.getWorth());
				} else if (ads.getType().equals("企业转债")) {
					saa.setCorporateConvertiblBond(saa
							.getCorporateConvertiblBond() + ads.getWorth());
				} else if (ads.getType().equals("创业板")) {
					saa.setTSXV(saa.getTSXV() + ads.getWorth());
				} else if (ads.getType().equals("投资基金")) {
					saa.setMarketFund(saa.getMarketFund() + ads.getWorth());
				} else if (ads.getType().equals("普通申购")) {
					saa.setNormalSubscribe(saa.getNormalSubscribe()
							+ ads.getWorth());
				} else if (ads.getType().equals("申购回款")) {
					saa.setSubscribeReceivedPayments(saa
							.getSubscribeReceivedPayments() + ads.getWorth());
				} else if (ads.getType().equals("股票")) {
					saa.setStock(saa.getStock() + ads.getWorth());
				} else if (ads.getType().equals("记账国债")) {
					saa.setBookingPublicDebts(saa.getBookingPublicDebts()
							+ ads.getWorth());
				} else if (ads.getType().equals("信用")) {
					saa.setCreditStock(saa.getCreditStock() + ads.getWorth());
				} else {
					saa.setOther(saa.getOther() + ads.getWorth());
				}
			}
		}
		this.logUserAction("StaffAccountAssertReportAction", "query:"
				+ this.type + this.start + "<->" + this.end + " in the "
				+ this.valueDay);
		return SUCCESS;
	}

	public Date getStart() {
		return start;
	}

	public Date getEnd() {
		return end;
	}

	public Date getValueDay() {
		return valueDay;
	}

	public String getType() {
		return type;
	}

	public List<DataDictionary> getTypeList() {
		return typeList;
	}

	public List<Account> getAccountList() {
		return accountList;
	}

	public DataDictionaryManager getDataDictionaryManager() {
		return dataDictionaryManager;
	}

	public AccountManager getAccountManager() {
		return accountManager;
	}

	public StaffManager getStaffManager() {
		return staffManager;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public void setValueDay(Date valueDay) {
		this.valueDay = valueDay;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setTypeList(List<DataDictionary> typeList) {
		this.typeList = typeList;
	}

	public void setAccountList(List<Account> accountList) {
		this.accountList = accountList;
	}

	public void setDataDictionaryManager(
			DataDictionaryManager dataDictionaryManager) {
		this.dataDictionaryManager = dataDictionaryManager;
	}

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public void setStaffManager(StaffManager staffManager) {
		this.staffManager = staffManager;
	}

	public void setStaffAccountAssertMap(
			HashMap<Staff, StaffAccountAssert> staffAccountAssertMap) {
		this.staffAccountAssertMap = staffAccountAssertMap;
	}

	public HashMap<Staff, StaffAccountAssert> getStaffAccountAssertMap() {
		return staffAccountAssertMap;
	}

	public void setAccountDayFundManager(
			AccountDayFundManager accountDayFundManager) {
		this.accountDayFundManager = accountDayFundManager;
	}

	public AccountDayFundManager getAccountDayFundManager() {
		return accountDayFundManager;
	}

	public void setAccountDayStockManager(
			AccountDayStockManager accountDayStockManager) {
		this.accountDayStockManager = accountDayStockManager;
	}

	public AccountDayStockManager getAccountDayStockManager() {
		return accountDayStockManager;
	}

	public AccountDayMoneyManager getAccountDayMoneyManager() {
		return accountDayMoneyManager;
	}

	public void setAccountDayMoneyManager(
			AccountDayMoneyManager accountDayMoneyManager) {
		this.accountDayMoneyManager = accountDayMoneyManager;
	}
}